package com.yanqu.road.dao.impl.activity.oldplayerreturn;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.oldplayerreturn.OldPlayerReturnUserData;
import com.yanqu.road.entity.activity.oldplayerreturn.OldReturnWhite;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/12/7 11:23
 */
public class OldPlayerReturnUserDataDaoImpl extends TempDataObjectDao<OldPlayerReturnUserData> {
    private static String addSql = "insert into t_u_role_return(userId,activityId,activityType,newServerId,newUserId," +
            "newTime,newUserBaseInfo,sendReturnConditionRewardState,oldServerId,oldUserId,oldTime,oldUserBaseInfo,vipExp," +
            "returnVipExpConfig,sendBindConditionRewardState,sendRewardDays,sendBindRewardOver,isReturn,returnTime," +
            "lastRefreshTime,isGetEventReward,eventId,placeId,whiteBindEffect,gotCardTrialTime,hadBuyCheapCard,rmbMallLimit) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(OldPlayerReturnUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityType()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNewServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNewUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNewTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getNewUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isSendReturnConditionRewardState()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOldServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOldUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOldTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getOldUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getVipExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReturnVipExpConfig()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isSendBindConditionRewardState()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSendRewardDays()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isSendBindRewardOver()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReturn()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReturnTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRefreshTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetEventReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getPlaceId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isWhiteBindEffect()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGotCardTrialTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isHadBuyCheapCard()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRmbMallLimit())));

        if (getDb().executeNoneQuery(addSql, para) > 0) {
            return true;
        }
        return false;
    }

    private static String updateSql = "update t_u_role_return set activityId=?,activityType=?,newServerId=?,newUserId=?," +
            "newTime=?,newUserBaseInfo=?,sendReturnConditionRewardState=?,oldServerId=?,oldUserId=?,oldTime=?,oldUserBaseInfo=?," +
            "vipExp=?,returnVipExpConfig=?,sendBindConditionRewardState=?,sendRewardDays=?,sendBindRewardOver=?,isReturn=?," +
            "returnTime=?,lastRefreshTime=?,isGetEventReward=?,eventId=?,placeId=?,whiteBindEffect=?,gotCardTrialTime=?,hadBuyCheapCard=?,rmbMallLimit=? where userId=?";

    @Override
    protected boolean updateData(OldPlayerReturnUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityType()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNewServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNewUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNewTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getNewUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isSendReturnConditionRewardState()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOldServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOldUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOldTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getOldUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getVipExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReturnVipExpConfig()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isSendBindConditionRewardState()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSendRewardDays()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isSendBindRewardOver()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReturn()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReturnTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRefreshTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetEventReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getPlaceId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isWhiteBindEffect()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGotCardTrialTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isHadBuyCheapCard()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRmbMallLimit())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected OldPlayerReturnUserData parseData(ResultSet rs) throws SQLException {
        OldPlayerReturnUserData data = new OldPlayerReturnUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setActivityType(rs.getInt("activityType"));
        data.setUserId(rs.getLong("userId"));
        data.setNewServerId(rs.getLong("newServerId"));
        data.setNewUserId(rs.getLong("newUserId"));
        data.setNewTime(rs.getLong("newTime"));
        data.setNewUserBaseInfo(JSONObject.parseObject(rs.getString("newUserBaseInfo"), new TypeReference<UserBaseInfo>() {
        }));
        data.setSendReturnConditionRewardState(rs.getBoolean("sendReturnConditionRewardState"));
        data.setOldServerId(rs.getLong("oldServerId"));
        data.setOldUserId(rs.getLong("oldUserId"));
        data.setOldTime(rs.getLong("oldTime"));
        data.setOldUserBaseInfo(JSONObject.parseObject(rs.getString("oldUserBaseInfo"), new TypeReference<UserBaseInfo>() {
        }));
        data.setVipExp(rs.getLong("vipExp"));
        data.setReturnVipExpConfig(rs.getString("returnVipExpConfig"));
        data.setSendBindConditionRewardState(rs.getBoolean("sendBindConditionRewardState"));
        data.setSendRewardDays(rs.getInt("sendRewardDays"));
        data.setSendBindRewardOver(rs.getBoolean("sendBindRewardOver"));
        data.setReturn(rs.getBoolean("isReturn"));
        data.setReturnTime(rs.getLong("returnTime"));
        data.setLastRefreshTime(rs.getLong("lastRefreshTime"));
        data.setGetEventReward(rs.getBoolean("isGetEventReward"));
        data.setEventId(rs.getInt("eventId"));
        data.setPlaceId(rs.getString("placeId"));
        data.setWhiteBindEffect(rs.getBoolean("whiteBindEffect"));
        data.setGotCardTrialTime(rs.getLong("gotCardTrialTime"));
        data.setHadBuyCheapCard(rs.getBoolean("hadBuyCheapCard"));
        String strRmbMallLimit = rs.getString("rmbMallLimit");
        Map<Integer,Integer> rmbMallLimitMap;
        if(StringUtils.isNullOrEmpty(strRmbMallLimit)){
            rmbMallLimitMap = new ConcurrentHashMap<>();
        }else {
            rmbMallLimitMap = JSON.parseObject(rs.getString("rmbMallLimit"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {
            });
        }
        if(null == rmbMallLimitMap){
            rmbMallLimitMap = new ConcurrentHashMap<>();
        }
        data.setRmbMallLimit(rmbMallLimitMap);
        return data;
    }

    private static String getSql = "select * from t_u_role_return";

    public Map<Long, Map<Integer, OldPlayerReturnUserData>> getUserDataMap() {
        Map<Long, Map<Integer, OldPlayerReturnUserData>> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            rs = call.executeQuery();
            while (rs.next()) {
                OldPlayerReturnUserData data = parse(rs);
                if (!dataMap.containsKey(data.getUserId())) {
                    dataMap.put(data.getUserId(), new ConcurrentHashMap<>());
                }
                dataMap.get(data.getUserId()).put(data.getActivityType(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Long, Map<Integer, OldReturnWhite>> selectOldReturnWhiteMap() {
        String getSql = "select * from t_s_bl_white_list";
        Map<Long, Map<Integer, OldReturnWhite>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSql);
            rs = call.executeQuery();
            while (rs.next()) {
                OldReturnWhite data = parseOldReturnWhite(rs);
                if (!map.containsKey(data.getPlayerId())) {
                    map.put(data.getPlayerId(), new ConcurrentHashMap<>());
                }
                map.get(data.getPlayerId()).put(data.getWhiteType(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private OldReturnWhite parseOldReturnWhite(ResultSet rs) throws SQLException {
        OldReturnWhite data = new OldReturnWhite();
        data.setPlayerId(rs.getLong("userId"));
        data.setReturnVipExp(rs.getLong("vipExp"));
        data.setWhiteType(rs.getInt("type"));
        return data;
    }
}
